package com.lagou.config.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static com.lagou.common.constant.Constant.TOKEN_NAME;
import static com.lagou.common.constant.Constant.TOKEN_VALUE;

/**
 * @author tengfei wang
 * @version 1.0
 * @date Created in 2020/9/17
 */
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String token = (String) session.getAttribute(TOKEN_NAME);
        if (TOKEN_VALUE.equals(token)) {
            return true;
        }
        response.sendRedirect("/jpa/login");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }
}
